package com.kitty.game.school.service;

import com.kitty.game.config.GameMap;
import com.kitty.game.role.model.Role;
import com.kitty.game.config.NPC;
import com.kitty.common.utils.SpringUtils;
import com.kitty.game.npc.NpcDataPool;
import com.kitty.game.school.SchoolDataPool;
import com.kitty.game.school.model.SchoolSet;
import com.kitty.game.map.service.MapService;
import org.nutz.dao.Cnd;
import org.nutz.dao.Dao;
import org.nutz.lang.Strings;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class SchoolService {
    /**
     * 从数据库加载师门产品数据
     */
    public void loadSchoolSet() {
        Map<Byte, SchoolSet> schoolMap = new HashMap<>();

        Dao dao = SpringUtils.getBean(Dao.class);
        //List<SchoolSet> list = dao.query(SchoolSet.class, Cnd.NEW());
        List<SchoolSet> list = new ArrayList<>();
        {
            SchoolSet schoolSet = new SchoolSet();
            schoolSet.setType((byte)1);
            schoolSet.setTypeName("金");
            schoolSet.setInnerMap(10001);
            schoolSet.setMaster(14563);
            schoolSet.setTeacher(14565);
            schoolSet.setStudent(14564);
            schoolSet.setOuterMap(10000);
            schoolSet.setZhangmen(14567);
            list.add(schoolSet);
        }
        {
            SchoolSet schoolSet = new SchoolSet();
            schoolSet.setType((byte)2);
            schoolSet.setTypeName("木");
            schoolSet.setInnerMap(14001);
            schoolSet.setMaster(14611);
            schoolSet.setTeacher(14613);
            schoolSet.setStudent(14612);
            schoolSet.setOuterMap(14000);
            schoolSet.setZhangmen(14609);
            list.add(schoolSet);
        }
        {
            SchoolSet schoolSet = new SchoolSet();
            schoolSet.setType((byte)3);
            schoolSet.setTypeName("水");
            schoolSet.setInnerMap(15001);
            schoolSet.setMaster(13920);
            schoolSet.setTeacher(13922);
            schoolSet.setStudent(13921);
            schoolSet.setOuterMap(15000);
            schoolSet.setZhangmen(13916);
            list.add(schoolSet);
        }
        {
            SchoolSet schoolSet = new SchoolSet();
            schoolSet.setType((byte)4);
            schoolSet.setTypeName("火");
            schoolSet.setInnerMap(13001);
            schoolSet.setMaster(13784);
            schoolSet.setTeacher(13786);
            schoolSet.setStudent(13785);
            schoolSet.setOuterMap(13000);
            schoolSet.setZhangmen(13788);
            list.add(schoolSet);
        }
        {
            SchoolSet schoolSet = new SchoolSet();
            schoolSet.setType((byte)5);
            schoolSet.setTypeName("土");
            schoolSet.setInnerMap(16001);
            schoolSet.setMaster(13908);
            schoolSet.setTeacher(13910);
            schoolSet.setStudent(13909);
            schoolSet.setOuterMap(16000);
            schoolSet.setZhangmen(13871);
            list.add(schoolSet);
        }
        for (SchoolSet schoolSet : list) {
            schoolMap.put(schoolSet.getType(), schoolSet);
        }
        SchoolDataPool.schoolMap = schoolMap;
    }

    /**根据师门类型获得师门数据*/
    public SchoolSet getShoolSet(byte schoolType) {
        return SchoolDataPool.schoolMap.get(schoolType);
    }

    /**获得师门山头地图数据*/
    public GameMap getOuterMap(Role role) {
        short menpai = role.getPolar();

        SchoolSet shoolSet = getShoolSet((byte) menpai);
        if (shoolSet == null) {
            return null;
        }

        return SpringUtils.getBean(MapService.class).getMap(shoolSet.getOuterMap());
    }

    /**获得师门洞府地图数据*/
    public GameMap getInnerMap(Role role) {
        short menpai = role.getPolar();

        SchoolSet shoolSet = getShoolSet((byte) menpai);
        if (shoolSet == null) {
            return null;
        }

        return SpringUtils.getBean(MapService.class).getMap(shoolSet.getInnerMap());
    }

    /**获得与其对战的门派*/
    public byte getToFightSchool(byte selfShool) {
        return SchoolDataPool.taskToFightMap.get(selfShool);
    }

    /*public String replaceSchoolDesc(Role role, String info) {
        if (role == null) {return info;}
        return replaceSchoolDesc((byte)role.getPolar(), info);
    }*/

    public String replaceSchoolDesc(byte school, String info) {
        if (SchoolDataPool.schoolMap.containsKey(school) == false || Strings.isEmpty(info)) {return info;}
        //TODO 后面可以用正则式处理
        String temp = info;

        SchoolService schoolService = SpringUtils.getBean(SchoolService.class);
        SchoolSet selfShoolSet = schoolService.getShoolSet(school);

        MapService mapService = SpringUtils.getBean(MapService.class);
        temp = temp.replace("{self_school_guang_name}", selfShoolSet.getTypeName());
        temp = temp.replace("{self_school_shan_name}", mapService.getMap(selfShoolSet.getOuterMap()).getName());
        temp = temp.replace("{self_school_dong_name}", mapService.getMap(selfShoolSet.getInnerMap()).getName());
        temp = temp.replace("{self_school_tong_zi_name}", mapService.getNpc(selfShoolSet.getStudent()).getName());
        temp = temp.replace("{self_school_zhang_lao_name}", mapService.getNpc(selfShoolSet.getTeacher()).getName());
        temp = temp.replace("{self_school_tian_zun_name}", mapService.getNpc(selfShoolSet.getMaster()).getName());


        SchoolSet otherShoolSet = schoolService.getShoolSet(schoolService.getToFightSchool(school));
        if (otherShoolSet == null) {
            // TODO 找不到时，理论上是有异常
            temp = temp.replace("{other_school_guang_name}", "");
            temp = temp.replace("{other_school_shan_name}", "");
            temp = temp.replace("{other_school_dong_name}", "");
            temp = temp.replace("{other_school_tong_zi_name}", "");
            temp = temp.replace("{other_school_zhang_lao_name}", "");
            temp = temp.replace("{other_school_tian_zun_name}", "");
        } else {
            temp = temp.replace("{other_school_guang_name}", otherShoolSet.getTypeName());
            temp = temp.replace("{other_school_shan_name}", mapService.getMap(otherShoolSet.getOuterMap()).getName());
            temp = temp.replace("{other_school_dong_name}", mapService.getMap(otherShoolSet.getInnerMap()).getName());
            temp = temp.replace("{other_school_tong_zi_name}", mapService.getNpc(otherShoolSet.getStudent()).getName());
            temp = temp.replace("{other_school_zhang_lao_name}", mapService.getNpc(otherShoolSet.getTeacher()).getName());
            temp = temp.replace("{other_school_tian_zun_name}", mapService.getNpc(otherShoolSet.getMaster()).getName());
        }

        return temp;
    }

    /**初始化*/
    public void initTaskSchoolNpc(NPC npc) {
        /**需要先初始化门派数据*/
        if (SchoolDataPool.schoolMap == null) {
            loadSchoolSet();
        }

        Map<Integer, Map<Byte, NPC>> taskSchoolNpcMap = new HashMap<>();
        Map<String, NPC> taskSchoolNpcMapByName = new HashMap<>();

        MapService mapService = SpringUtils.getBean(MapService.class);
//        Map<Byte, NPC> npcMap = new HashMap<>();
//        Map<Byte, NPC> npcMapByName = new HashMap();
        try {
            for (Map.Entry<Byte, Byte> entry : SchoolDataPool.taskSchoolSexMap.entrySet()) {
                byte school = entry.getKey();
                byte sex = entry.getValue();

                NPC cloneNpc = npc.clone();
                SchoolSet shoolSet = getShoolSet(school);
                cloneNpc.setName(mapService.getMap(shoolSet.getInnerMap()).getName()+npc.getName());

                short icon = mapService.heroIcon(sex, school);
                cloneNpc.setIcon(icon);

                Map<Byte, NPC> npcMap = taskSchoolNpcMap.get(cloneNpc.getId());
                if (npcMap == null) {
                    npcMap = new HashMap<>();
                    taskSchoolNpcMap.put(cloneNpc.getId(), npcMap);
                }
                npcMap.put(school, cloneNpc);

                taskSchoolNpcMapByName.put(cloneNpc.getName(), cloneNpc);
            }
        } catch (CloneNotSupportedException e) {
            e.printStackTrace();
        }

        NpcDataPool.taskSchoolNpcMap = taskSchoolNpcMap;
        NpcDataPool.taskSchoolNpcMapByName = taskSchoolNpcMapByName;
    }

    public NPC getNpc(String npcName) {
        return NpcDataPool.taskSchoolNpcMapByName.get(npcName);
    }

    public NPC getNpc(int npcId, byte school) {
        Map<Byte, NPC> npcMap = NpcDataPool.taskSchoolNpcMap.get(npcId);
        if (npcMap != null) {
            return npcMap.get(school);
        }

        return null;
    }

    public byte getToFightTaskSex(byte school) {
        return SchoolDataPool.taskSchoolSexMap.get(school);
    }

    /**获得任务中与其对战的门派使用技能id列表*/
    public List<Integer> getToFightTaskSkillIds(byte school) {
        byte toFightSchool = getToFightSchool(school);
        List<Integer> skillIds = new ArrayList<>();
        List<Integer> tempSkillIds = SchoolDataPool.taskSchoolSkillMap.get(toFightSchool);
        if (tempSkillIds != null) {
            skillIds.addAll(tempSkillIds);
        }
        return skillIds;
    }

    public short getSchoolWeaponIcon(byte school) {
        return SchoolDataPool.taskSchoolWeaponMap.get(school);
    }
}
